package com.dao.impl;

import com.bean.Bhcasetm;
import com.bean.Bhtopic;
import com.bean.TestRecords;
import com.bean.Usecasetrue;
import com.dao.UsecasetrueDao;
import com.util.AliPool;
import com.util.StringManipulation;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.poi.util.IntList;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class UsecasetrueDaoImpl implements UsecasetrueDao {
    QueryRunner runner =new QueryRunner();

    @Override
    public List<Usecasetrue> queryUsecasetrue(String sql, Object... obj) throws Exception {
        List<Usecasetrue> list = null;
        System.out.println(sql);
        Connection conn= AliPool.getConn();
        list=runner.query(conn,sql,new BeanListHandler<Usecasetrue>(Usecasetrue.class),obj);
        System.out.println(list);
        conn.close();
        return list;
    }

    @Override
    public int addDA(String whereuID, String id, String test){
        Connection conn= AliPool.getConn();
        Usecasetrue use = null;
        int i=0;
        try {
            String sql = "SELECT * FROM Usecasetrue where cUrl=?";
            System.out.println(sql);
            use=runner.query(conn,sql,new BeanListHandler<Usecasetrue>(Usecasetrue.class),whereuID).get(0);
            sql="UPDATE Usecasetrue SET cTNum=?,cTrue=? where cUrl=?";
            if(use.getcTrue()!=null){
                i=runner.update(conn,sql,use.getcTrue()+"\\"+id,use.getcTNum()+"\\"+test,whereuID);
            }else{
                i=runner.update(conn,sql,id,test,whereuID);
            }
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int addUSum(String sql) {//
        Connection conn= AliPool.getConn();
        int i=0;
        try {
            Usecasetrue use=this.queryUsecasetrue(sql).get(0);
            String ctrue[] = StringManipulation.stringFanXieGang(use.getcTrue());//答案
            Bhcasetm tm = new BhcasetmDaoImpl().queryBhcasetm("SELECT * FROM bhcasetm where cUrl=?",use.getcUrl()).get(0);
            String cid[] =  StringManipulation.stringDouHao(tm.getcID());//题目id
            String cnum[] = StringManipulation.stringDouHao(tm.getcNum());//题目分数
            String str;
            for (int k=0;k<ctrue.length;k++){
                try {
                    Bhtopic list = new  BhtopicDaoImpl().queryBhtopic("SELECT * FROM bhtopic where topIcID=?",cid[k]).get(0);
                    System.out.println("#"+list.getcTrue()+"="+ctrue[k]+"#");
                    if(list.getcTrue().equalsIgnoreCase(ctrue[k])){
                        i+= Integer.valueOf(cnum[Integer.valueOf(k)]);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            //修改分数
            sql="UPDATE Usecasetrue SET uSum=? where cUrl=? and uID=?";
            runner.update(conn,sql,i,use.getcUrl(),use.getuID());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                conn.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return i;
    }

    @Override
    public List<Integer> getmap(String sql) {
        System.out.println(sql);
        Connection conn= AliPool.getConn();
        List<Integer> intlist = null;
        try {
            Usecasetrue use=this.queryUsecasetrue(sql).get(0);
            String ctrue[] = StringManipulation.stringFanXieGang(use.getcTrue());//答案
            Bhcasetm tm = new BhcasetmDaoImpl().queryBhcasetm("SELECT * FROM bhcasetm where cUrl=?",use.getcUrl()).get(0);
            String cid[] =  StringManipulation.stringDouHao(tm.getcID());//题目id
            String cnum[] = StringManipulation.stringDouHao(tm.getcNum());//题目分数
            String str;
            intlist = new ArrayList<Integer>();
            for (int k=0;k<ctrue.length;k++){
                try {
                    Bhtopic list = new  BhtopicDaoImpl().queryBhtopic("SELECT * FROM bhtopic where topIcID=?",cid[k]).get(0);
                    System.out.println("#"+list.getcTrue()+"="+ctrue[k]+"#");
                    if(list.getcTrue().equalsIgnoreCase(ctrue[k])){
                        intlist.add(1);
                    }else{
                        intlist.add(0);
                    }
                }catch (Exception e){
                    intlist.add(0);
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                conn.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return intlist;
    }


    @Override
    public List<TestRecords> selTestRecords(int uid, int page) {
        String sql="select cName,uSum from usecasetrue a INNER JOIN bhcase b ON a.cUrl=b.cUrl where a.uid=? LIMIT ?,10";
        Connection conn= AliPool.getConn();
        List<TestRecords> list = null;
        try {
            list = runner.query(conn,sql, new BeanListHandler<TestRecords>(TestRecords.class),uid,page-1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
    public  int dudas(Usecasetrue usecasetrue)throws Exception{
        String sql="insert into usecasetrue(cUrl,uID,cTrue) values(?,?,?)";
        Connection conn= AliPool.getConn();
        Object object[]=new Object[3];
        object[0]=usecasetrue.getcUrl();
        object[1]=usecasetrue.getuID();
        object[2]=usecasetrue.getcTrue();
        int a=runner.update(conn,sql,object);
        return a;
    }
}
